#!/bin/bash

#ifcfg_name=`ip addr|grep '^[0-9]'|grep -vE "lo|virbr|docker0|vnet0"|sort|head -1|awk -F ":| " '{print $3}'`
ifcfg_name=`ip addr|grep '^[0-9]'|grep -vE "lo|virbr|docker0|vnet0"|head -1|awk -F ":| " '{print $3}'`
#echo $ifcfg_name

#remove ifcfg file

ls /etc/sysconfig/network-scripts/ifcfg-*|grep -Ev "lo|back"|xargs rm -f 


cp ifg-back-eth0 /etc/sysconfig/network-scripts/
cp ifg-back-br0 /etc/sysconfig/network-scripts/ifcfg-br0
cp ifg-back-br0 /etc/sysconfig/network-scripts/ifcfg-br0:1

sed -i 's/br0/br0:1/g' /etc/sysconfig/network-scripts/ifcfg-br0:1
sed -i '/^IPADDR=/c IPADDR=192.168.10.1' /etc/sysconfig/network-scripts/ifcfg-br0:1
sed -i '/^GATEWAY=/c GATEWAY=' /etc/sysconfig/network-scripts/ifcfg-br0:1
sed -i '/^TYPE=/c #TYPE=' /etc/sysconfig/network-scripts/ifcfg-br0:1
sed -i '/^DNS1=/c DNS1=' /etc/sysconfig/network-scripts/ifcfg-br0:1


real_ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-$ifcfg_name"
first_ifcfg_file=$real_ifcfg_file

#modify file
cp /etc/sysconfig/network-scripts/ifg-back-eth0 $first_ifcfg_file
sed -i '/^NAME=/c NAME='$ifcfg_name'' $first_ifcfg_file
sed -i '/^DEVICE=/c DEVICE='$ifcfg_name'' $first_ifcfg_file


service network restart

